linux c++ 利用timerfd和epoll封装计时器(Timer)类 您所在的位置:网站首页 linux c 计时器 linux c++ 利用timerfd和epoll封装计时器(Timer)类

linux c++ 利用timerfd和epoll封装计时器(Timer)类

2024-07-04 20:30| 来源: 网络整理| 查看: 265

ref:linux c++ 利用timerfd和epoll封装计时器(Timer)类_sumkee911‘s coding-CSDN博客

程序简介:

1.把timerfd和epoll的功能封装成一个类,timerfd负责创建计时器,而epoll负责等待timer超时,然后调用用户设定得回调函数。

2.至于timerfd的基本功能不明白,就参考这位大神的技术文章:blog.csdn.net/chgaowei/article/details/21295811

3.至于epoll的基本功能不明白,就参考这位大神得技术文章:blog.csdn.net/xiajun07061225/article/details/9250579

Timer.h  

#ifndef __TIMER_H_ #define __TIMER_H_ /* * Name: Timer * Date: 10-12-2015 * Author: Sumkee * Brief: It's general timer that provides a friendly interfaces * to create the timer * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; class Timer { public: Timer(); ~Timer(); // The structure of timer event typedef void (*CALLBACK_FN)(void *); typedef struct _TimerEvent { int fd; CALLBACK_FN cbf; void *args; } TimerEvent; /* * Name: start * Brief: start the timer * @interval: The interval, the unit is ms * @cbf: The callback function * @args: The arguments of callback function * @triggered_on_start: Determine tirggered on start or not * */ bool start(const uint interval, CALLBACK_FN cbf, void *args,const bool triggered_on_start=false); /* * Name: stop * Brief: stop the timer * */ void stop(); private: bool m_is_start; TimerEvent m_te; }; #endif

Timer.cpp

#include "timer.h" /* * Save the global data such as file descriptors of timerfd and * create a new thread for epoll * */ class TimerPrivate { public: TimerPrivate(); ~TimerPrivate() { pthread_mutex_destroy(&m_mutex); } // Some constant enum { MaxEPOLLSize = 20000, }; /* * Name: epoll_proc * Brief: this function run on new thread for epoll * */ static void* epoll_proc(void *); /* * Get the timer event by fd * */ static Timer::TimerEvent get_timer_event(int fd); /* * Add the timer event to map and epoll * */ static bool add_timer_event(const Timer::TimerEvent &te); /* * Remove the timer event from map adn epoll by fd * */ static void remove_timer_event(const int fd); // Map of file descriptor int m_epoll_fd; typedef map MapTimerEvent; MapTimerEvent m_map_te; pthread_t m_tid; pthread_mutex_t m_mutex; }; // The declare of TimerPrivate static TimerPrivate g_tp; TimerPrivate::TimerPrivate() { try { // Initialization // Init mutex int res = pthread_mutex_init(&m_mutex, 0); if(res == -1) { perror("pthread_mutex_init"); throw; } // Create epoll m_epoll_fd = epoll_create(MaxEPOLLSize); if(m_epoll_fd == -1) { perror("epoll_create"); throw; } // Create thread for epoll res = pthread_create(&m_tid, 0, TimerPrivate::epoll_proc, 0); if(res == -1) { perror("pthread_create"); throw; } } catch (...) {} } void* TimerPrivate::epoll_proc(void *) { struct epoll_event events[MaxEPOLLSize]; while(1) { // Wait for notice int n =epoll_wait(g_tp.m_epoll_fd, events, MaxEPOLLSize, -1); pthread_mutex_lock(&g_tp.m_mutex); for(int i=0; i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有